<?php

/**
 * This is the model class for table "tb_horario".
 *
 * The followings are the available columns in table 'tb_horario':
 * @property integer $idtb_horario
 * @property string $Fecha
 * @property string $HoraInicio
 * @property string $HoraFin
 * @property string $tiempo
 * @property string $tb_usuarios_login
 *
 * The followings are the available model relations:
 * @property TbUsuarios $tbUsuariosLogin
 */
class TbHorarioTotal extends CActiveRecord
{
	public $accionActual;
	public $usuarioActual;
	public $fechaActual;
	public function obtenerUsuarios(){
		return (CHtml::listData(TbUsuarios::model()->findAll(),'login','nombreCompleto'));
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return TbHorarioTotal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_horario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Fecha, HoraInicio, tb_usuarios_login', 'required'),
			array('tb_usuarios_login', 'length', 'max'=>50),
			array('HoraFin, tiempo', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idtb_horario, Fecha, HoraInicio, HoraFin, tiempo, tb_usuarios_login', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbUsuariosLogin' => array(self::BELONGS_TO, 'TbUsuarios', 'tb_usuarios_login'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idtb_horario' => 'Id',
			'Fecha' => 'Fecha',
			'HoraInicio' => 'Hora Inicio',
			'HoraFin' => 'Hora Fin',
			'tiempo' => 'Tiempo',
			'tb_usuarios_login' => 'Usuario',
			'fechaActual'=>'Fecha',
			'usuarioActual'=>'Usuario',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idtb_horario',$this->idtb_horario);
		$criteria->compare('Fecha',$this->Fecha,true);
		$criteria->compare('HoraInicio',$this->HoraInicio,true);
		$criteria->compare('HoraFin',$this->HoraFin,true);
		$criteria->compare('tiempo',$this->tiempo,true);
		$criteria->compare('tb_usuarios_login',$this->tb_usuarios_login,true);
		
		$criteria->order='Fecha DESC';
		$criteria->group='Fecha, tb_usuarios_login';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function afterFind(){
		parent::afterFind();
		$h = new TbUtility();
		$this->fechaActual = $h->AcomodarFormatoFecha($this->Fecha);
		$this->tiempo = $h->convertirHoras($this->tiempo);
		$u = TbUsuarios::model()->findByPk($this->tb_usuarios_login);
		$this->usuarioActual = $u->nombreCompleto;
	}
}